//
// Created by kaifamiao on 2022/4/6.
//
#ifndef STRING_CLASS_CSTRING_H
#define STRING_CLASS_CSTRING_H
#endif //STRING_CLASS_CSTRING_H

#include <cstdio>
#include <iostream>
#include <vector>
#include<io.h>
#include <cstring>
#include<algorithm> //for_each
#include <fstream>
struct StockD {
    unsigned int stock_date;
    unsigned int stock_open;
    unsigned int stock_high;
    unsigned int stock_low;
    unsigned int stock_close;
    unsigned int stock_amount;
    unsigned int stock_vol;
    unsigned int stock_reservation;
};
using namespace std;
namespace str {
    string buwei(string s){
        if(s.length()<2)
            return "0"+s;
        else
            return s;
    }
    string getdate() {
        time_t rawtime;
        struct tm *ptminfo;
        time(&rawtime);
        ptminfo = localtime(&rawtime);
        return to_string(ptminfo->tm_year + 1900) +"-"+ buwei(to_string(ptminfo->tm_mon + 1)) +"-"+ buwei(to_string(ptminfo->tm_mday));
        //, , ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
        //gett();
    }
    string gettime() {
        time_t rawtime;
        struct tm *ptminfo;
        time(&rawtime);
        ptminfo = localtime(&rawtime);

        return to_string(ptminfo->tm_hour ) +":"+ to_string(ptminfo->tm_min) +":"+ to_string(ptminfo->tm_sec);
        //, , ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
        //gett();
    }
    string getweek() {
        time_t rawtime;
        struct tm *ptminfo;
        time(&rawtime);
        ptminfo = localtime(&rawtime);

        return to_string(ptminfo->tm_wday);
        //, , ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
        //gett();
    }
    string now(){
        return getdate()+" "+ gettime();
    }

    void str_to_array(const string &old_str, char flag ,string new_str[]) {
        int s = 0;
        for (char i: old_str) {
            if (i == flag) {
                s++;
                continue;
            }
            new_str[s] += i;
        }
    }

    template <typename T>
    string to_str(T str){
        return to_string(str);
    }

    string toupper(string tostr) {
        transform(tostr.begin(), tostr.end(), tostr.begin(), ::toupper);
        return tostr;
    }

    string tolower(string tostr) {
        transform(tostr.begin(), tostr.end(), tostr.begin(), ::tolower);
        return tostr;
    }

    //取得字符串长度,用strlen也可以代替
    int len(const string &cstr) {
        return cstr.length();
    }

    string strcolor(string ch, string str) {
        return ch + str + "\033[0m";
    }

    //从右边取自定义个数字符格式(目标，源，数量)
    string right(string fromstr, int start = 0, int num = 1) {
        int i;
        string to_str, to_tmp;
        int StatrNum = fromstr.length() - num - (start);
        if (num > fromstr.length()) {
            return "function rigth string " + fromstr + ",length error!";
        }
        for (i = 0; i < num; i++) {
            to_str[i] = fromstr[i + StatrNum];
            to_tmp += to_str[i];
        }
        return to_tmp;
    }

    //从左边取自定义个数字符格式(目标，源，数量)
    string left(string fromstr, int start = 0, int num = 1) {
        string to;
        string tmp_str;
        int i;
        if (num > fromstr.length()) {
            return "function rigth string " + fromstr + ",length error!";
        }
        for (i = start; i < num; i++) {
            to[i] = fromstr[i];
            tmp_str += to[i];
        }
        return tmp_str;
    }
    string reverse(string strs){
        reverse(strs.begin(),strs.end());
        return strs;
    }
    string replace(const string &oldstr, const char flag, const string repstr) {
        int s = 0;
        string newstr;
        for (char i: oldstr) {
            if (i == flag) {
                s++;
                newstr += repstr;
                continue;
            }
            newstr += i;
        }
        return newstr;
    }


    string replace( string &str, const string flag, const string repstr) {
        int pos;
        pos = str.find(flag);
        while(pos != -1){
            // str.length()求字符的长度，注意str必须是string类型
            str.replace(pos,string(flag).length(),repstr);
            pos = str.find(flag);
        }
        return str;
    }
    /* 字符串截取 */
    string getSubString(string s,int pos,int n){
        // substr(pos)函数的使用，返回从pos开始(包含pos位置的字符)所有的字符
//    cout << str.substr(2) << endl;
//    // substr(pos,n)，返回从pos开始(包含pos位置的字符)n个字符
//    cout << str.substr(pos, n) << endl;
        return s.substr(pos,n);
    }
    //从左边查找有无匹配字符串
    int Instr(string Txt, const string Word) {
        int i, s;
        int WordNum = Word.length();
        int TxtNum = Txt.length();
        int InstrNum = 0;
        if (TxtNum > WordNum) {
            for (i = 0; i < TxtNum; i++) {
                if (s = WordNum) {
                    InstrNum = i;
                    break;
                }
                for (s = 0; s < WordNum; s++, i++) {

                    if (Word[s] == Txt[i]) {
                        continue;
                    } else {
                        if (InstrNum == 0)
                            InstrNum = 0;
                        cout <<s <<endl;
                        break;
                    }

                }
            }
//if(InstrNum!=0)
//InstrNum=InstrNum-WordNum;
        }
        return InstrNum;
    }

//从右边查找有无匹配字符串
    int InstrRev(string Txt, string Word) {
        int i, s;
        int WordNum = Word.length();
        int TxtNum = Txt.length();
        int InstrRevNum = 0;
        if (TxtNum > WordNum) {
            for (i = TxtNum; i > 0; i--) {
                InstrRevNum = i;
                for (s = WordNum; s > 0; s--, i--) {
                    if (Word[s] == Txt[i]) {
                        continue;
                    } else
                        break;
                }
            }
//InstrNum=TxtNum-(InstrNum+WordNum)+1;
        }
        return InstrRevNum;
    }

}
/* 字符串查找 */
void findSubString(string str){
    // find()函数的使用，返回查找对象第一次出现的位置.
    cout << str.find("fs") << endl;
    // rfind()函数的使用，返回查找对象最后出现的位置
    cout << str.rfind("s") << endl;
}

namespace file{

    vector<string> readfile_to_vector(string path){
        ifstream inf;
        inf.open(path,ios::in);
        if(!inf.is_open()){
            cerr << "open file " << path << "error!"<<endl;
            exit(1);
        }
        vector<string> vec;
        char fdata[200];
        while(!inf.eof()){
            inf.getline(fdata,sizeof(fdata));
            vec.emplace_back(fdata);
        }
        inf.clear();
        return vec;
    }
    template <typename T>
    vector<T> readfile_to_vector(string path,T fdata){
        ifstream inf;
        inf.open(path,ios::in | ios::binary);
        if(!inf.is_open()){
            cerr << "open file " << path << "error!"<<endl;
            exit(1);
        }
        vector<T> vec;
        while(!inf.eof()){
            inf.read((char*)&fdata, sizeof(T));
            vec.emplace_back(fdata);
        }
        inf.clear();
        return vec;
    }
}

/* 字符串替换 */
void replaceString(string str){
    // replace(pos,n,s1)，用s1替换从pos开始的n个字符
    cout << str.replace(0,2,"xiaoming") << endl;
}

/*

class CString {
public:
    CString &operator=(const CString &cn);//重构=
    CString operator+(const CString &cn);//重构+
    char operator[](int index);//重构[]
    bool operator<(const CString &cn);//重构<
    bool operator>(const CString &cn);//重构>
    bool operator==(const CString &cn);//重构==
    CString();//默认构造函数
    CString(const char *s);//构造函数
    CString(const CString &c);//拷贝构造函数
    ~CString();//析构函数
    friend ostream &operator<<(ostream &os, const CString &obj) {
        os << obj.m_s;
        return os;
    }

    void display();//输出数据
public:
    char *m_s;
};


//重载运算符：=
CString &CString::operator=(const CString &cn) {
    this->m_s = new char[strlen(cn.m_s) + 1];
    strcpy(this->m_s, cn.m_s);
    return *this;
}

//重载运算符：+
CString CString::operator+(const CString &cn) {
    CString temp;
    temp.m_s = new char[strlen(this->m_s) + strlen(cn.m_s) + 1];
    strcpy(temp.m_s, this->m_s);
    strcat(temp.m_s, cn.m_s);
    return temp;
}

//重载运算符：[]
char CString::operator[](int index) {
    if (index >= strlen(this->m_s)) {
        return '\0';
    }
    return this->m_s[index];
}

//重载运算符：<
bool CString::operator<(const CString &cn) {
    if (strcmp(this->m_s, cn.m_s) < 0) {
        return true;
    }
    return false;
}

//重载运算符：>
bool CString::operator>(const CString &cn) {
    if (strcmp(this->m_s, cn.m_s) > 0) {
        return true;
    }
    return false;
}

//重载运算符：==
bool CString::operator==(const CString &cn) {
    if (strcmp(this->m_s, cn.m_s) == 0) {
        return true;
    }
    return false;
}

//默认构造函数
CString::CString() {
    this->m_s = new char('\0');
}

//构造函数
CString::CString(const char *s) {
    m_s = new char[strlen(s) + 1];
    strcpy(m_s, s);
}

//拷贝构造函数
CString::CString(const CString &c) {
    m_s = new char[strlen(c.m_s) + 1];
    strcpy(m_s, c.m_s);
}

//析构函数
CString::~CString() {
    delete m_s;
    m_s = nullptr;
}

//输出数据
void CString::display() {
    std::cout << m_s << std::endl;
}*/
